using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TemplateCreateTools
{
    public partial class MainForm : Form
    {
        private DataCenter dataCenter; 
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            dataCenter = new DataCenter();
        }

        private void btn_set_path_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.SelectedPath = Environment.CurrentDirectory;
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                dataCenter.Initialize(fbd.SelectedPath);
                pbar.Minimum = 0;
                pbar.Maximum = dataCenter.Count;
                showData();
            }
        }
        private void showData()
        {
            lsb_NG.Items.Clear();
            lsb_OK.Items.Clear();
            foreach (SourceItem item in dataCenter.NG)
            {
                lsb_NG.Items.Add(item.FileName);
            }
            foreach (SourceItem item in dataCenter.OK)
            {
                lsb_OK.Items.Add(item.FileName);
            }
        }

        private void lsb_NG_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lsb_NG.SelectedIndex < dataCenter.NG.Count)
            {
                SourceItem item = (SourceItem)dataCenter.NG[lsb_NG.SelectedIndex];
                rtb_srouce.Text = item.Source;
            }
        }

        private void lst_OK_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lsb_OK.SelectedIndex < dataCenter.OK.Count)
            {
                SourceItem item = (SourceItem)dataCenter.OK[lsb_OK.SelectedIndex];
                rtb_srouce.Text = item.Source;
            }
        }

        private void btn_test_Click(object sender, EventArgs e)
        {
            dataCenter.NG.Clear();
            dataCenter.OK.Clear();
            lsb_NG.Items.Clear();
            lsb_OK.Items.Clear();
            pbar.Value =0;
            string template = rtb_template.Text.Replace("\n","\r\n");
            foreach (SourceItem item in dataCenter.All)
            {
                pbar.Value++;
                if (item.Judge(template))
                {
                    dataCenter.OK.Add(item);
                    lsb_OK.Items.Add(item.FileName);
                }
                else
                {
                    dataCenter.NG.Add(item);
                    lsb_NG.Items.Add(item.FileName);
                }
            }
        }

    }
}